<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:gml="http://www.opengis.net/gml" 
	xmlns:ogr="http://ogr.maptools.org/"
	xmlns:ramon_geo="http://ec.europa.eu/eurostat/ramon/ontologies/geographic.rdf#" 
	xmlns:ramon="http://rdfdata.eionet.europa.eu/ramon/ontology/"
	xmlns:fao="http://aims.fao.org/aos/geopolitical.owl#"
	xmlns:owl="http://www.w3.org/2002/07/owl#"
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<xsl:output method="xml" omit-xml-declaration="yes" doctype-system="http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd" doctype-public="-//W3C//DTD XHTML+RDFa 1.0//EN" indent="yes"/>
	
<xsl:template match="/ogr:FeatureCollection">

	<xsl:variable name="dbpedia_dump" select="document('../sources/dbpedia/nuts_dbpedia_settlements_over_10000.xml')"/>
	<xsl:variable name="nuts_dump" select="document('../sources/ramon/nuts2008.rdf')"/>
	<xsl:variable name="eionet_dump" select="document('../sources/eionet/eionet.rdf')"/>	
	<xsl:variable name="fao_dump" select="document('../sources/fao/geopolitical.owl')"/>	
	<xsl:variable name="ukos_mappings" select="document('../sources/ukos/ukos_nuts_seeAlso.rdf')"/>	
	<xsl:variable name="geoes_mappings" select="document('../sources/geoes/geoes_nuts_seeAlso.rdf')"/>	
	<xsl:variable name="touches_relations" select="document('../sources/touches.xml')"/>	

	<xsl:for-each select='gml:featureMember/ogr:NUTS_RG_60M_2006' >
	
		<xsl:variable name="nuts_id" select="ogr:NUTS_ID"/>
		<xsl:variable name="nuts_uri" select="concat('http://nuts.geovocab.org/id/',$nuts_id)"/>
		<xsl:variable name="eionet_uri" select="concat('http://rdfdata.eionet.europa.eu/eea/countries/',$nuts_id)"/>
		<xsl:variable name="nuts_node" select="$nuts_dump/rdf:RDF/ramon_geo:NUTSRegion[@rdf:about=$nuts_id]"/>
		<xsl:variable name="eionet_node" select="$eionet_dump/rdf:RDF/rdf:Description[@rdf:about=$eionet_uri]"/>
		<xsl:variable name="fao_node" select="$fao_dump/rdf:RDF/fao:self_governing[fao:codeISO2=$nuts_id]"/>

		<xsl:result-document href="{concat('id/',ogr:NUTS_ID,'.php')}">

			<html> 	
			<head> 
			  <title><xsl:value-of select="$nuts_node/rdfs:label" /></title>
			  <link rel="alternate" type="text/turtle" href="{concat(ogr:NUTS_ID,'.ttl')}"/>
			  <link rel="alternate" type="application/rdf+xml" href="{concat(ogr:NUTS_ID,'.rdf')}"/>
			  <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
			  <link rel="stylesheet" type="text/css" href="../css/style.css"/> 
			</head> 

			<body> 


			    <div id="container">
			      <xsl:processing-instruction name="php">
				  include('../header.php');
			      </xsl:processing-instruction>
			      <div id="content">
				<h1><xsl:value-of select="$nuts_node/rdfs:label" /></h1>
				<p><i>This file is a human-readable representation of both the <a href="{concat('http://nuts.geovocab.org/id/',$nuts_id)}">feature</a>
				and its <a href="{concat('http://nuts.geovocab.org/id/',$nuts_id,'_geometry')}">geometry</a>.</i></p>
				
				<p>View the feature as: <a href="{concat($nuts_id,'.ttl')}">Turtle</a>, <a href="{concat($nuts_id,'.rdf')}">RDF/XML</a></p>
				
				<p>View the geometry as: <a href="{concat($nuts_id,'_geometry.ttl')}">Turtle</a>, <a href="{concat($nuts_id,'_geometry.rdf')}">RDF/XML</a>, 
					<a href="{concat($nuts_id,'_geometry.gml')}">GML</a>, <a href="{concat($nuts_id,'_geometry.kml')}">KML</a></p>
				
				<div style="clear:both; float:right;">
				<script src="{concat('http://www.gmodules.com/ig/ifr?url=http://code.google.com/apis/kml/embed/embedkmlgadget.xml&amp;up_kml_url=http%3A%2F%2Fnuts.geovocab.org%2Fid%2F',$nuts_id,'_geometry.kml&amp;up_view_mode=maps&amp;up_earth_2d_fallback=0&amp;up_earth_fly_from_space=1&amp;up_earth_show_nav_controls=1&amp;up_earth_show_buildings=1&amp;up_earth_show_terrain=1&amp;up_earth_show_roads=1&amp;up_earth_show_borders=1&amp;up_earth_sphere=earth&amp;up_maps_zoom_out=0&amp;up_maps_default_type=map&amp;synd=open&amp;w=500&amp;h=400&amp;title=Embedded+KML+Viewer&amp;border=%23ffffff%7C3px%2C1px+solid+%23999999&amp;output=js')}"></script>
				</div>
				
				<h2 id="feature">Feature</h2>
				<table>
					<xsl:apply-templates select="ogr:geometryProperty">
						<xsl:with-param name="dbpedia_nodes" select="$dbpedia_dump/table/row[column[position()=1]=$nuts_uri]" />
						<xsl:with-param name="nuts_node" select="$nuts_node" />
						<xsl:with-param name="eionet_node" select="$eionet_node" />
						<xsl:with-param name="fao_node" select="$fao_node" />
						<xsl:with-param name="ukos_node" select="$ukos_mappings/rdf:RDF/rdf:Description[@rdf:about=$nuts_uri]"/>	
						<xsl:with-param name="geoes_node" select="$geoes_mappings/rdf:RDF/rdf:Description[@rdf:about=$nuts_uri]"/>	
						<xsl:with-param name="touches_nodes" select="$touches_relations/table/row[column[position()=1]=$nuts_uri]" />
						<xsl:with-param name="nuts_id" select="$nuts_id" />
					</xsl:apply-templates>
				</table>
				
				<h2 id="geometry">Geometry</h2>
				<table>
					<xsl:apply-templates select="ogr:geometryProperty/child::node()"/>
				</table>
			      </div>
			    </div> 
			    <xsl:processing-instruction name="php">
				  include('../footer.php');
			    </xsl:processing-instruction>

			</body> 
			</html> 
			
		</xsl:result-document>
		
	</xsl:for-each>

</xsl:template>

<xsl:template name="geometry" match="ogr:geometryProperty">
	<xsl:param name="dbpedia_nodes" />
	<xsl:param name="nuts_node" />
	<xsl:param name="eionet_node" />
	<xsl:param name="fao_node" />
	<xsl:param name="ukos_node" />
	<xsl:param name="geoes_node" />
	<xsl:param name="touches_nodes" />	
	<xsl:param name="nuts_id" />
	

	<tr>
		<th>rdf:type</th>
		<td><a href="http://rdfdata.eionet.europa.eu/ramon/ontology/NUTSRegion">ramon:NUTSRegion</a>, <a href="http://geovocab.org/spatial#Feature">spatial:Feature</a></td>
	</tr>
	<tr>
		<th>rdfs:label</th>
		<td><xsl:value-of select="$nuts_node/rdfs:label"/></td>
	</tr>
	<tr>
		<th>ramon:name</th>
		<td><xsl:value-of select="$nuts_node/ramon_geo:name" /></td>
	</tr>
	<tr>
		<th>ramon:level</th>
		<td><xsl:value-of select="$nuts_node/ramon_geo:level" /></td>
	</tr>
	<tr>
		<th>ramon:code</th>
		<td><xsl:value-of select="$nuts_node/ramon_geo:code" /></td>
	</tr>
	<tr>
		<th>ngeo:geometry</th>
		<td><a href="{concat('http://nuts.geovocab.org/id/',$nuts_id,'_geometry')}">nuts:<xsl:value-of select="$nuts_id"/>_geometry</a></td>
	</tr>
	<xsl:if test="$nuts_node/ramon_geo:hasParentRegion">
		<xsl:variable name="pp_id" select="$nuts_node/ramon_geo:hasParentRegion/@rdf:resource"/>
		<tr>
			<th>spatial:PP</th>
			<td><a href="{concat('http://nuts.geovocab.org/id/',$pp_id)}"><xsl:value-of select="$pp_id" /></a></td>
		</tr>
	</xsl:if>
	<xsl:if test="$fao_node">
		<xsl:for-each select='$fao_node/fao:hasBorderWith' >
			<xsl:variable name="ec_node" select="substring-after(./@rdf:resource, '#')"/>
			<xsl:variable name="codeISO2" select="../../fao:self_governing[@rdf:ID=$ec_node]/fao:codeISO2"/>
			<xsl:if test="$nuts_node/../ramon_geo:NUTSRegion[@rdf:about=$codeISO2]">
				<xsl:variable name="ec_id" select="../../fao:self_governing[@rdf:ID=$ec_node]/fao:codeISO2"/>
				<tr>
					<th>spatial:EC</th>
					<td><a href="{concat('http://nuts.geovocab.org/id/',$ec_id)}">nuts:<xsl:value-of select="$ec_id" /></a></td>
				</tr>
			</xsl:if>
		</xsl:for-each>
	</xsl:if>
	<xsl:if test="$dbpedia_nodes">
		<xsl:for-each select='$dbpedia_nodes' >
			<tr>
				<th>spatial:PPi</th>
				<td><a href="{column[position()=last()]}"><xsl:value-of select="column[position()=last()]" /></a></td>
			</tr>
		</xsl:for-each>
	</xsl:if>
	<xsl:if test="$touches_nodes">
		<xsl:for-each select='$touches_nodes' >
			<tr>
				<th>spatial:EC</th>
				<td><a href="{column[position()=last()]}"><xsl:value-of select="column[position()=last()]" /></a></td>
			</tr>
		</xsl:for-each>
	</xsl:if>
	<xsl:variable name="uri" select="concat('http://estatwrap.ontologycentral.com/dic/geo#',$nuts_id)"/>
	<tr>
		<th>owl:sameAs</th>
		<td><a href="{$uri}"><xsl:value-of select="$uri" /></a></td>
	</tr>

	<xsl:variable name="uri" select="concat('http://rdfdata.eionet.europa.eu/ramon/nuts2008/',$nuts_id)"/>
	<tr>
		<th>owl:sameAs</th>
		<td><a href="{$uri}"><xsl:value-of select="$uri" /></a></td>
	</tr>

	<xsl:variable name="uri" select="concat('http://ec.europa.eu/eurostat/ramon/rdfdata/nuts2008/',$nuts_id)"/>
	<tr>
		<th>owl:sameAs</th>
		<td><a href="{$uri}"><xsl:value-of select="$uri" /></a></td>
	</tr>

	<xsl:variable name="uri" select="concat('http://nuts.psi.enakting.org/id/',$nuts_id)"/>
	<tr>
		<th>owl:sameAs</th>
		<td><a href="{$uri}"><xsl:value-of select="$uri" /></a></td>
	</tr>		

	<xsl:if test="$eionet_node">
		<xsl:for-each select='$eionet_node/owl:sameAs' >
			<tr>
				<th>owl:sameAs</th>
				<td><a href="{./@rdf:resource}"><xsl:value-of select="./@rdf:resource" /></a></td>
			</tr>
		</xsl:for-each>
	</xsl:if>
	<xsl:if test="$ukos_node">
		<xsl:for-each select='$ukos_node/rdfs:seeAlso' >
			<tr>
				<th>rdfs:seeAlso</th>
				<td><a href="{./@rdf:resource}"><xsl:value-of select="./@rdf:resource" /></a></td>
			</tr>
		</xsl:for-each>
	</xsl:if>
	<xsl:if test="$geoes_node">
		<xsl:for-each select='$geoes_node/rdfs:seeAlso' >
			<tr>
				<th>rdfs:seeAlso</th>
				<td><a href="{./@rdf:resource}"><xsl:value-of select="./@rdf:resource" /></a></td>
			</tr>
		</xsl:for-each>
	</xsl:if>
</xsl:template>


<xsl:template match="gml:MultiPolygon|gml:MultiSurface">
	<tr>
		<th>rdf:type</th>
		<td><a href="http://geovocab.org/geometry#MultiPolygon">ngeo:MultiPolygon</a></td>
	</tr>
	<xsl:apply-templates select="child::gml:polygonMember|child::gml:surfaceMember"/>
</xsl:template>

<xsl:template match="gml:polygonMember|gml:surfaceMember">
	<tr>
		<th>ngeo:polygonMember</th>
		<td><table><xsl:apply-templates select="child::gml:Polygon|child::gml:Surface"/></table></td>
	</tr>
</xsl:template>

<xsl:template name="polygon" match="gml:Polygon|gml:Surface">
	<tr>
		<th>rdf:type</th>
		<td><a href="http://geovocab.org/geometry#Polygon">ngeo:Polygon</a></td>
	</tr>
	<xsl:apply-templates select="gml:interior|gml:innerBoundaryIs|gml:exterior|gml:outerBoundaryIs"/>
</xsl:template>

<xsl:template match="gml:interior|gml:innerBoundaryIs">
	<tr>
		<th>ngeo:interior</th>
		<td><table><xsl:apply-templates select="child::gml:LinearRing"/></table></td>
	</tr>
</xsl:template>

<xsl:template match="gml:exterior|gml:outerBoundaryIs">
	<tr>
		<th>ngeo:exterior</th>
		<td><table><xsl:apply-templates select="child::gml:LinearRing"/></table></td>
	</tr>
</xsl:template>

<xsl:template match="gml:LinearRing">
	<tr>
		<th>rdf:type</th>
		<td><a href="http://geovocab.org/geometry#LinearRing">ngeo:LinearRing</a></td>
	</tr>	
	<xsl:apply-templates select="child::gml:posList|child::gml:coordinates"/>
</xsl:template>

<xsl:template match="gml:LineString|gml:Curve">
	<tr>
		<th>rdf:type</th>
		<td><a href="http://geovocab.org/geometry#LineString">ngeo:LineString</a></td>
	</tr>
	<xsl:apply-templates select="child::gml:posList|child::gml:coordinates"/>
</xsl:template>

<xsl:template match="gml:posList|gml:coordinates">
	<tr>
		<th>ngeo:posList</th>
		<td>
			<table>
				<xsl:call-template name="format-posList">
					<xsl:with-param name="text" select="self::node()"/>
				</xsl:call-template>
			</table>
		</td>
	</tr>
</xsl:template>

<xsl:template name="format-posList">
  <xsl:param name="text"/>
  <xsl:choose>
    <xsl:when test="not(contains($text, ','))">
      <xsl:value-of select="$text"/>
    </xsl:when>
    <xsl:otherwise>
		<tr><th>geo:long</th><td><xsl:value-of select="substring-before($text, ',')"/></td></tr>
		<xsl:call-template name="format-posList-close">
			<xsl:with-param name="text" select="substring-after($text, ',')"/>
		</xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="format-posList-close">
  <xsl:param name="text"/>
  <xsl:choose>
    <xsl:when test="not(contains($text, ' '))">
      <tr><th>geo:lat</th><td><xsl:value-of select="$text"/></td></tr>
    </xsl:when>
    <xsl:otherwise>
      <tr><th style="padding-bottom: 7px;">geo:lat</th><td><xsl:value-of select="substring-before($text, ' ')"/></td></tr>
      <xsl:call-template name="format-posList">
        <xsl:with-param name="text" select="substring-after($text, ' ')"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>